var form,$,table,layer,upload,node,layedit;
var user = JSON.parse(localStorage.getItem("user"));
var userId = user.userId;
var draftMicrocircle,layeditIndex,artcleUrl,microcircleId,article;
layui.use(['form','jquery','table','layer','upload','layedit'],function() {
    form = layui.form;
    $ = layui.jquery;
    table = layui.table;
    layer = layui.layer;
    upload = layui.upload;
    layedit = layui.layedit;
    //富文本编辑器上传图片
    layedit.set({
        uploadImage: {
            url: '/microcircle/upload',
            type: 'post'
        }
    });

    layeditIndex = layedit.build("area",{
        height:300
    });

    //默认展示草稿（2：草稿状态）
    showDraf(2);

});

//选择文章分类
$("#articleClassification").on("click",function () {
    layer.open({
        type:2,
        title:"文章分类",
        area:["500px","300px"],
        content:"/microCircle/article-classification.html",
    });
})

function showDraf(details) {
    $.ajax({
        url:"/microcircle/getDraftMicrocircle",
        type: "post",
        dataType: "json",
        data: {"details": details},
        success: function (data) {
            draftMicrocircle = data.data;
            if (data.data != null){
                //草稿状态微圈页面赋值
                $("input[name=artcleTitle]").val(data.data.artcleTitle);
                $("input[name=artcleCategroyName]").val(data.data.artcleCategroyName);
                $("input[name=acticleCategroyId]").val(data.data.acticleCategroyId);
                $("input[name=artcleUrl]").val(data.data.artcleUrl);
                $("input[name=microcircleId]").val(data.data.microcircleId);
                //为富文本编辑器赋值
                layedit.setContent(layeditIndex, data.data.article, false);
            }else{
                $("input[name=artcleTitle]").val('');
                $("input[name=artcleCategroyName]").val('');
                $("input[name=acticleCategroyId]").val('');
                $("input[name=artcleUrl]").val('');
                $("input[name=microcircleId]").val('');
                layedit.setContent(layeditIndex, '', false);
            }
        }
    });
}

//保存草稿
$("#draft").on("click",function () {
    var artcleTitle = $("input[name=artcleTitle]").val();
    var acticleCategroyId = $("input[name=acticleCategroyId]").val();
    if (draftMicrocircle != null){
        microcircleId = draftMicrocircle.microcircleId;
    }
    article = layedit.getContent(layeditIndex);
    var article2 = layedit.getContent(layeditIndex);
    var strStart = article2.indexOf('"') + 1;
    var strEnd = article2.indexOf("alt") - 2;
    var artcleUrl = article2.substring(strStart,strEnd);
    var data = {
        userId: userId,
        artcleTitle: artcleTitle,
        acticleCategroyId: acticleCategroyId,
        microcircleId: microcircleId,
        article: article,
        artcleUrl: artcleUrl,
        updateId: userId,
        details: 2,//草稿
    };
    //保存草稿（有草稿时修改草稿）
    if (draftMicrocircle != null){
        $.ajax({
            url: "/microcircle/updateDraftMicrocircle",
            type: "post",
            dataType: "json",
            data: data,
            success: function (data) {
                if (data.data){
                    showDraf(2);
                    addLog(3,4,'修改草稿');//3：微圈 4：修改
                    layer.msg("保存成功");
                }
            }
        });
    }else{
        //无草稿时添加草稿
        if (acticleCategroyId != '' && artcleTitle != '' && article != '') {
            $.ajax({
                url: "/microcircle/addDraftMirList",
                type: "post",
                dataType: "json",
                data: data,
                success: function (data) {
                    microcircleId = data.data.microcircleId;
                    if (data.data){
                        showDraf(2);
                        addLog(3,2,'添加草稿');//3：微圈 2：添加
                        layer.msg("保存成功");
                    }
                }
            });
        }else {
            layer.msg("请补充信息");
        }
    }
});

//发布微圈
$("#release").on("click",function () {
    var artcleTitleText = $("input[name=artcleTitle]").val();
    var artcleCategroyNameText = $("input[name=artcleCategroyName]").val();
    var articleText = layedit.getContent(layeditIndex);
    if (artcleTitleText != '' && artcleCategroyNameText != '' && articleText != ''){
        var artcleUrl = $("input[name=artcleUrl]").val();
        var microcircleId = $("input[name=microcircleId]").val();
        if (draftMicrocircle != null){
            var data = {
                productId: microcircleId,
                microcircleImgUrl: artcleUrl,
                microcircleImgType: 3,//微圈图片
                createId: userId,
            }
            //添加微圈图片表
            $.ajax({
                url: "/microcircle/addMirImg",
                type: "post",
                dataType: "json",
                data: data,
                success: function (data) {
                    if (data.data){
                        showDraf(2);
                        addLog(3,2,'添加微圈');//3：微圈 2：添加
                        layer.msg("发布成功");
                    }
                }
            });
        }else{
            var artcleTitle = $("input[name=artcleTitle]").val();
            var acticleCategroyId = $("input[name=acticleCategroyId]").val();
            if (draftMicrocircle != null){
                microcircleId = draftMicrocircle.microcircleId;
            }
            var article = layedit.getContent(layeditIndex);
            var article2 = layedit.getContent(layeditIndex);
            var strStart = article2.indexOf('"') + 1;
            var strEnd = article2.indexOf("alt") - 2;
            var artcleUrl = article2.substring(strStart,strEnd);
            var data = {
                userId: userId,
                artcleTitle: artcleTitle,
                acticleCategroyId: acticleCategroyId,
                microcircleId: microcircleId,
                article: article,
                artcleUrl: artcleUrl,
                updateId: userId,
                details: 5,//已提交
            };
            $.ajax({
                url: "/microcircle/addMicrocircle",
                type: "post",
                dataType: "json",
                data: data,
                success: function (data) {
                    if (data.data){
                        showDraf(2);
                        addLog(3,2,'添加微圈');//3：微圈 2：添加
                        layer.msg("发布成功");
                    }
                }
            });
        }
    }else{
        layer.msg("请补充内容");
    }
});

//添加操作日志
function addLog(logType,type,remark) {
    $.ajax({
        url: "/microcircle/addLog",
        dataType: "json",
        type: "post",
        data: {"logType":logType,"type":type,"userId":userId,"remark":remark},
        success: function (data) {

        }
    });
}

//返回首页
function backOff(){
    parent.jumpSystemHome();
}